package com.jjb.www.ui.fragment;

import android.os.Bundle;

import androidx.recyclerview.widget.GridLayoutManager;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.shape.layout.ShapeRecyclerView;
import com.jjb.www.R;
import com.jjb.www.app.AppAdapter;
import com.jjb.www.app.AppFragment;
import com.jjb.www.http.api.HomeCourseListApi;
import com.jjb.www.http.api.HomeCourseListApi;
import com.jjb.www.http.model.HttpListData;
import com.jjb.www.other.AppConfig;
import com.jjb.www.ui.activity.CopyActivity;
import com.jjb.www.ui.adapter.CourseListAdapter;
import com.jjb.www.widget.CommonRefreshView;

import java.util.List;

import okhttp3.Call;

/**
 * author : Android CRG
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/10/18
 * desc   : 首页课程列表
 */
public final class JPCourseListFragment extends AppFragment<CopyActivity> {
    private CommonRefreshView mRefreshView;
    private CourseListAdapter mAdapter;
    private static final String INTENT_KEY_IN_ID = "ID";

    public static JPCourseListFragment newInstance(String id) {
        JPCourseListFragment fragment = new JPCourseListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(INTENT_KEY_IN_ID, id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.jp_course_list_fragment;
    }

    @Override
    protected void initView() {
        mRefreshView = findViewById(R.id.refreshView);
        mAdapter = new CourseListAdapter(getContext());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        mRefreshView.setLayoutManager(gridLayoutManager);
    }

    @Override
    protected void initData() {
        getListData();
    }

    private void getListData() {
        mRefreshView.setDataHelper(new CommonRefreshView.DataHelper<HomeCourseListApi.Bean>() {
            @Override
            public AppAdapter<HomeCourseListApi.Bean> getAdapter() {
                return mAdapter;
            }

            @Override
            public void loadData(int p, HttpCallback<HttpListData<HomeCourseListApi.Bean>> callback) {
                EasyHttp.get(getAttachActivity())
                        .api(new HomeCourseListApi()
                                .setUser_id(AppConfig.getInstance().getUserBean().getUser().getId()+"")
                                .setEssence_id(getString(INTENT_KEY_IN_ID))
                                .setPage(p)
                                .setPageSize(mAdapter.getSize())
                        ).request(new HttpCallback<HttpListData<HomeCourseListApi.Bean>>(getAttachActivity()) {

                    @Override
                    public void onStart(Call call) {

                    }

                    @Override
                    public void onSucceed(HttpListData<HomeCourseListApi.Bean> result) {
                        callback.onSucceed(result);
                    }

                    @Override
                    public void onFail(Exception e) {
                        super.onFail(e);
                        callback.onFail(e);
                    }

                    @Override
                    public void onEnd(Call call) {
                        super.onEnd(call);
                        callback.onEnd(call);
                    }
                });
            }

            @Override
            public void onRefreshSuccess(List<HomeCourseListApi.Bean> list, int listCount) {

            }

            @Override
            public void onRefreshFailure() {

            }

            @Override
            public void onLoadMoreSuccess(List<HomeCourseListApi.Bean> loadItemList, int loadItemCount) {

            }

            @Override
            public void onLoadMoreFailure() {

            }
        });
        mRefreshView.initData();
    }
}